<template>
  <div class="PlayListDetail">
    <div class="pic">
      <van-icon name="arrow-left" @click="goBack" />
      <van-image width="100%" height="275px" :src="listPic" fit="cover" />
    </div>
    <div v-if="songUrl == ''" class="play">
      <div class="play_left">
        <img src="../../assets/play.png" @click="playSong" />
        <span>播放全部</span>
      </div>

      <div class="play_right">
        <span>收藏歌单</span>
        <img src="../../assets/play.png" />
      </div>
    </div>
    <div v-else class="play">
      <audio :src="songUrl" autoplay controls></audio>
    </div>
    <div class="songList">
      <ul>
        <li
          v-for="item in songList"
          :key="item.songid"
          @click="selectSong(item)"
        >
          <div class="song_item">
            <h4>{{ item.songname }}</h4>
            <span>{{ item.singer[0].name }} — {{ item.albumname }}</span>
          </div>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import axios from "axios";
export default {
  name: "PlayListDetail",
  data() {
    return {
      listPic: "",
      songList: [],
      songUrl: "",
    };
  },
  created() {
    // console.log(this.$route.params.id);
    this.getData();
  },
  methods: {
    getData() {
      axios
        .get("/api/songList", {
          params: {
            id: this.$route.params.id,
          },
        })
        .then((res) => {
          console.log(res.data.data.songlist[0]);
          this.listPic = res.data.data.logo;
          this.songList = res.data.data.songlist;
        })
        .catch((err) => {
          console.error(err);
        });
    },
    goBack() {
      this.$router.go(-1);
    },
    selectSong(item) {
      // console.log(item)
      axios
        .get("/jsososo/song/urls", {
          params: {
            id: item.songmid,
          },
        })
        .then((res) => {
          console.log(res.data.data[item.songmid]);
          this.songUrl = res.data.data[item.songmid];
        })
        .catch((err) => {
          console.error(err);
        });
    },
    playSong() {
      axios
        .get("/jsososo/song/urls", {
          params: {
            id: this.songList[0].songmid,
          },
        })
        .then((res) => {
          console.log(res.data.data[this.songList[0].songmid]);
          this.songUrl = res.data.data[this.songList[0].songmid];
        })
        .catch((err) => {
          console.error(err);
        });
    },
  },
};
</script>

<style scoped>
.van-icon {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 999;
  font-size: 30px;
}
li {
  border-top: 1px solid #ccc;
  padding: 5px 0;
}
.song_item {
  display: flex;
  flex-direction: column;
  margin: 0 10px;
}
.song_item h4 {
  margin: 10px 0;
}
.song_item > span {
  font-size: 10px;
  margin-bottom: 5px;
}
.play {
  display: flex;
  padding: 0 10px;
  height: 60px;
  /* position: relative; */
  text-align: center;
}
.play img {
  width: 30px;
}
.play_left {
  display: flex;
  align-items: center;
}
.play_right span {
  display: block;
}
.play_right {
  position: absolute;
  right: 10px;
}
</style>